package com.sds.shop.action;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sds.fw.mvc.Action;
import com.sds.fw.mvc.HttpUtil;
import com.sds.fw.util.DBUtil;
import com.sds.shop.vo.SessionVO;

public class InsertBoardAction extends Action{
	public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		try{
			System.out.println("aaa");
			
			SessionVO svo = (SessionVO)request.getSession().getAttribute("user");
			
			String mem_id = svo.getMemId();
			String title = (String)request.getParameter("title");
			System.out.println(title);
			String text = (String)request.getParameter("text");
			System.out.println(text);
			
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;

			conn = DBUtil.getConnection();
			ps = conn.prepareStatement("select max(to_number(board_no)) from tb_board");
			rs = ps.executeQuery();
			
			int board_no = 1;
			if(rs.next()) board_no = rs.getInt(1);
			
			String sql = "insert into tb_board values(?,?,?,?,?,?,sysdate,'')";		
			ps = conn.prepareStatement(sql); 
			ps.setInt(1, board_no+1);
			ps.setString(2, svo.getMemId());
			ps.setString(3, title);
			ps.setString(4, text);
			ps.setInt(5, 0);
			ps.setBoolean(6, false);
			ps.executeUpdate();
			
			rs.close();
			ps.close();
			conn.close();
			
			HttpUtil.forward(request, response, "/board.do");
		}
		catch(Exception ex){
			System.out.println("getGoods exception : " + ex);
		}
	}
}
